package com.phiture.erp.purchase.core.domain;

import com.phiture.erp.basic.api.pojo.resp.ErpUserCustomizeConfigResp;
import com.phiture.erp.common.constants.ErpUserCustomizeConfigKeyConstants;
import com.phiture.erp.common.enums.ErpAuditStatus;
import com.phiture.erp.common.enums.ErpCommonStatusEnum;
import com.phiture.erp.common.enums.PurchaseOrderMergeTypeEnum;
import lombok.Getter;

import java.util.Map;

/**
 * @author YH
 * @date 2025-06-03-16:10
 */

@Getter
public class ErpPurchaseConfig {
    private final Boolean allowCreatePurchaseOrder;
    private final int approvalStatus;
    private final Boolean allowMergePurchaseOrder;
    private final PurchaseOrderMergeTypeEnum mergeTypeEnum;

    public ErpPurchaseConfig(Map<String, ErpUserCustomizeConfigResp> map, boolean batchMode){
        ErpUserCustomizeConfigResp approvalResp = map.get(ErpUserCustomizeConfigKeyConstants.ERP_PURCHASE_REQUEST_DEFAULT_APPROVED);
        boolean autoApproved = Boolean.parseBoolean(approvalResp.getConfigValue());
        ErpUserCustomizeConfigResp autoCreatePurchaseResp = map.get(ErpUserCustomizeConfigKeyConstants.ERP_AUTO_CREATE_PURCHASE_ORDER);
        boolean autoCreatePurchaseOrder = Boolean.parseBoolean(autoCreatePurchaseResp.getConfigValue());
        this.allowCreatePurchaseOrder = autoApproved && autoCreatePurchaseOrder;
        this.approvalStatus = autoApproved ? ErpAuditStatus.APPROVE.getStatus() : ErpAuditStatus.PROCESS.getStatus();
        ErpUserCustomizeConfigResp mergePurchaseOrderResp = map.get(ErpUserCustomizeConfigKeyConstants.ERP_MERGE_PURCHASE_ORDERS);
        this.allowMergePurchaseOrder = ErpCommonStatusEnum.isOpen(mergePurchaseOrderResp.getStatus()) && batchMode;
        this.mergeTypeEnum = PurchaseOrderMergeTypeEnum.fromType(mergePurchaseOrderResp.getConfigValue());
    }

}
